﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Common;

namespace TaxiDispatchClient.Service
{
    public class DataTransCommon
    {
        //司机信息发送给乘客
        public byte[] SendCarInfoData(byte type,string carphoneNum,
            double lng, double lat,string tarGetPhoneNum)
        {
            try
            {
                string transferServerIp = OperateConfig.GetClientTransferServerIp();
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ipAddress = IPAddress.Parse(transferServerIp);
                IPEndPoint address = new IPEndPoint(ipAddress, ConstParameters.TransferListenPort);
                socket.Connect(address);
                socket.NoDelay = true;
                List<byte> mBytes = new List<byte>();
                //公用Id
                mBytes.AddRange(BitConverter.GetBytes(
                    ConstParameters.CmdSendByClientPublicId).Reverse());
                //目标手机号
                mBytes.AddRange(CommonRoutine.BcdStringToBytes(tarGetPhoneNum.PadLeft(12, '0')));
                //本机IP
                IPAddress localIp = ((IPEndPoint)(socket.LocalEndPoint)).Address;
                mBytes.AddRange(localIp.GetAddressBytes());
                //真实ID
                mBytes.AddRange(BitConverter.GetBytes(
                    ConstParameters.CmdSendCarAgreeOnInfoByClient).Reverse());
                //消息体

                mBytes.Add(type);
                //phoneNumber 6byte
                mBytes.AddRange(CommonRoutine.BcdStringToBytes(
                    carphoneNum.PadLeft(12, '0')));

                lng = Math.Round(lng, 6);
                lat = Math.Round(lat, 6);
                //longitude 5
                string longitudestr =
                    ((lng * Math.Pow(10, 6)).ToString().PadLeft(10, '0'));
                byte[] longitudeBytes = CommonRoutine.BcdStringToBytes(longitudestr);
                mBytes.AddRange(longitudeBytes);
                //latitude 5
                string latitudestr =
                    (lat * Math.Pow(10, 6)).ToString().PadLeft(10, '0');
                byte[] latitudeBytes = CommonRoutine.BcdStringToBytes(latitudestr);
                mBytes.AddRange(latitudeBytes);
                 
                byte[] messageHead = OperateAgreement.GetMessageHead(
                    ConstParameters.CmdRetweetTaxiDispatchClient, false,
                                                                     false, mBytes.ToArray().Length, new byte[6],
                                                                     new byte[2], 0, 0);
                byte[] sendData = OperateAgreement.PackageSendData(messageHead, mBytes.ToArray());
                socket.Send(sendData);
                Thread.Sleep(100);
                socket.ReceiveTimeout = 5000;

                byte[] buffer = new byte[100];
                 
                int receDataCount = socket.Receive(buffer);
                byte[] receData = new byte[receDataCount];
                Array.Copy(buffer, 0, receData, 0, receDataCount);
                if (receDataCount >= ConstParameters.MhLenth)
                {
                    socket.Close();
                    return receData;
                }
                return null;
            }
            catch (Exception ex)
            {                 
                CommonRoutine.HandlingExceptions(ex);
                return null;
            }
        }

        //乘客信息发送给司机
        public byte[] SendPassengerInfoData(string psgPhoneNumber,
            double lng, double lat, string tarGetPhoneNum)
        {
            try
            {
                string transferServerIp = OperateConfig.GetClientTransferServerIp();
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ipAddress = IPAddress.Parse(transferServerIp);
                IPEndPoint address = new IPEndPoint(ipAddress, ConstParameters.TransferListenPort);
                socket.Connect(address);
                socket.NoDelay = true;
                List<byte> mBytes = new List<byte>();
                //公用Id
                mBytes.AddRange(BitConverter.GetBytes(
                    ConstParameters.CmdSendByClientPublicId).Reverse());
                //目标手机号
                mBytes.AddRange(CommonRoutine.BcdStringToBytes(tarGetPhoneNum.PadLeft(12, '0')));
                //本机IP
                IPAddress localIp = ((IPEndPoint)(socket.LocalEndPoint)).Address;
                mBytes.AddRange(localIp.GetAddressBytes());
                //真实ID
                mBytes.AddRange(BitConverter.GetBytes(
                    ConstParameters.CmdSendPassengerAgreeOnInfoByClient).Reverse());
                //消息体

                //phoneNumber  6
                mBytes.AddRange(CommonRoutine.BcdStringToBytes(
                    psgPhoneNumber.PadLeft(12, '0')));

                lng = Math.Round(lng, 6);
                lat = Math.Round(lat, 6);
                //longitude 5
                string longitudestr =
                    ((lng * Math.Pow(10, 6)).ToString().PadLeft(10, '0'));
                byte[] longitudeBytes = CommonRoutine.BcdStringToBytes(longitudestr);
                mBytes.AddRange(longitudeBytes);
                //latitude 5
                string latitudestr =
                    (lat * Math.Pow(10, 6)).ToString().PadLeft(10, '0');
                byte[] latitudeBytes = CommonRoutine.BcdStringToBytes(latitudestr);
                mBytes.AddRange(latitudeBytes);

                byte[] messageHead = OperateAgreement.GetMessageHead(
                    ConstParameters.CmdRetweetTaxiDispatchClient, false,
                                                                     false, mBytes.ToArray().Length, new byte[6],
                                                                     new byte[2], 0, 0);
                byte[] sendData = OperateAgreement.PackageSendData(messageHead, mBytes.ToArray());
                socket.Send(sendData);
                Thread.Sleep(100);
                socket.ReceiveTimeout = 5000;

                byte[] buffer = new byte[100];

                int receDataCount = socket.Receive(buffer);
                byte[] receData = new byte[receDataCount];
                Array.Copy(buffer, 0, receData, 0, receDataCount);
                if (receDataCount >= ConstParameters.MhLenth)
                {
                    return receData;
                }
                return null;
            }
            catch (Exception ex)
            {
                CommonRoutine.HandlingExceptions(ex);
                return null;
            }
        }

        //发送目的地信息
        public byte[] SendDestinationInfoData(byte type, double lng,
            double lat, string name, string tarGetPhoneNum)
        {
            try
            {
                string transferServerIp = OperateConfig.GetClientTransferServerIp();
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ipAddress = IPAddress.Parse(transferServerIp);
                IPEndPoint address = new IPEndPoint(ipAddress, ConstParameters.TransferListenPort);
                socket.Connect(address);
                socket.NoDelay = true;
                List<byte> mBytes = new List<byte>();
                //公用Id
                mBytes.AddRange(BitConverter.GetBytes(
                    ConstParameters.CmdSendByClientPublicId).Reverse());
                //目标手机号
                mBytes.AddRange(CommonRoutine.BcdStringToBytes(tarGetPhoneNum.PadLeft(12, '0')));
                //本机IP
                IPAddress localIp = ((IPEndPoint)(socket.LocalEndPoint)).Address;
                mBytes.AddRange(localIp.GetAddressBytes());
                //真实ID
                mBytes.AddRange(BitConverter.GetBytes(
                    ConstParameters.CmdSendDestinationInfoByClient).Reverse());
                //消息体
                //typte  1
                mBytes.Add(type);
                lng = Math.Round(lng, 6);
                lat = Math.Round(lat, 6);
                //longitude 5
                string longitudestr =
                    ((lng * Math.Pow(10, 6)).ToString().PadLeft(10, '0'));
                byte[] longitudeBytes = CommonRoutine.BcdStringToBytes(longitudestr);
                mBytes.AddRange(longitudeBytes);
                //latitude 5
                string latitudestr =
                    (lat * Math.Pow(10, 6)).ToString().PadLeft(10, '0');
                byte[] latitudeBytes = CommonRoutine.BcdStringToBytes(latitudestr);
                mBytes.AddRange(latitudeBytes);
                mBytes.AddRange(ASCIIEncoding.GetEncoding("gbk").GetBytes(name));

                byte[] messageHead = OperateAgreement.GetMessageHead(
                    ConstParameters.CmdRetweetTaxiDispatchClient, false,
                                                                     false, mBytes.ToArray().Length, new byte[6],
                                                                     new byte[2], 0, 0);
                byte[] sendData = OperateAgreement.PackageSendData(messageHead, mBytes.ToArray());
                socket.Send(sendData);
                Thread.Sleep(100);
                socket.ReceiveTimeout = 10000;

                byte[] buffer = new byte[100];

                int receDataCount = socket.Receive(buffer);
                byte[] receData = new byte[receDataCount];
                Array.Copy(buffer, 0, receData, 0, receDataCount);
                if (receDataCount >= ConstParameters.MhLenth)
                {
                    return receData;
                }
                return null;
            }
            catch (Exception ex)
            {
                CommonRoutine.HandlingExceptions(ex);
                return null;
            }
        }
 
 
    }
}
